<template>
  <div>
    <!--    面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片视图-->
    <el-card class="box-card">
      <!--      搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.roleName" clearable @clear="getRightList">
            <el-button slot="append" icon="el-icon-search" @click="getRightList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAddRightDialog">添加权限</el-button>
        </el-col>
      </el-row>
    <!--      权限列表区域-->
      <el-table :data="rightsList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="权限名称" prop="roleName"></el-table-column>
        <el-table-column label="路径" prop="rolePath"></el-table-column>
        <el-table-column label="权限等级" prop="roleFatherId">
          <template v-slot="scope">
            <el-tag v-if="scope.row.roleFatherId==='0'">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.roleFatherId==='1'">二级</el-tag>
            <el-tag type="warning" v-else>三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template  v-slot="scope">
            <!--修改按钮  v-slot="scope"-->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.roleId)"></el-button>
            <!--            删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removerById(scope.row.roleId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--    添加权限的对话框 selectFatherRoleList-->
    <el-dialog
      title="添加权限"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed">
      <!--     内容主题 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="权限名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="权限父名称" prop="roleFatherId">
          <el-select v-model="addForm.roleFatherId" placeholder="请选择">
            <el-option
              v-for="item in selectFatherRoleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路径" prop="rolePath">
          <el-input v-model="addForm.rolePath"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="roleBase01">
          <el-input v-model="addForm.roleBase01"></el-input>
        </el-form-item>
      </el-form>
      <!--      底部信息-->
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addRightInfo">确 定</el-button>
  </span>
    </el-dialog>
    <!--    修改用户信息的对话框selectFatherRoleList-->
    <el-dialog
      title="编辑用户信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed">
      <!--     内容主题 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="权限名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="权限父名称" prop="roleFatherId">
          <el-select v-model="editForm.roleFatherId" placeholder="请选择">
            <el-option
              v-for="item in selectFatherRoleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路径" prop="rolePath">
          <el-input v-model="editForm.rolePath"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="roleBase01">
          <el-input v-model="editForm.roleBase01"></el-input>
        </el-form-item>
      </el-form>
      <!--      底部信息-->
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editRightInfo">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      //  获取用户列表的参数对象
      queryInfo: {
        // 获取用户列表的参数对象
        roleName: '',
        // 当前的页数
        pageNum: 1,
        pageSize: 10
      },
      // 权限列表
      rightsList: [],
      total: 0,
      // 控制添加权限弹框的展示与隐藏
      addDialogVisible: false,
      // 控制编辑权限弹框的展示与隐藏
      editDialogVisible: false,
      //  添加用户的表单数据
      addForm: {
        roleName: '',
        roleFatherId: '',
        rolePath: '',
        roleBase01: ''
      },
      //  根据id查询到的用户信息对象
      editForm: {},
      // 添加表单的验证规则对象
      addFormRules: {
        roleName: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        roleFatherId: [
          { required: true, message: '请输入父名称', trigger: 'blur' }
        ]
      },
      editFormRules: {
        roleName: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        roleFatherId: [
          { required: true, message: '请输入父名称', trigger: 'blur' }
        ]
      },
      // 一级权限列表
      selectFatherRoleList: []
    }
  },
  created () {
    // 获取所有权限
    this.getRightList()
  },
  methods: {
    // 获取权限列表
    async getRightList () {
      const { data: res } = await this.$http.get('getroles', { params: this.queryInfo })
      if (res.status !== 200) {
        return this.$message.error('获取权限列表失败！')
      }
      this.rightsList = res.data.list
      this.total = res.data.total
    },
    // 监听pageSize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getRightList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getRightList()
    },
    // 监听添加权限对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    //  监听修改用户信息的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 添加权限
    addRightInfo () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        const { data: res } = await this.$http.post('addrole', this.addForm)
        if (res.status !== 200) {
          return this.$message.error(res.msg)
        }
        this.$message.success('添加权限成功！')
        //  隐藏添加用户的对话框
        this.addDialogVisible = false
        // 获取用户列表信息
        this.getRightList()
        // 刷新页面
        location.reload()
      })
    },
    //  根据id删除对应的权限信息
    async removerById (roleId) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该权限以及其子权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      //  如果用户确认删除，则返回值为字符串 confirm
      //  如果用户取消了删除，则返回值为字符串cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消')
      }
      const { data: res } = await this.$http.get('deleterolebyid', { params: { roleId: roleId } })
      if (res.status !== 200) {
        return this.$message.error('删除权限失败！')
      }
      this.$message.success('删除权限成功！')
      this.getRightList()
      // 刷新页面
      location.reload()
    },
    // 展示添加权限的对话框
    showAddRightDialog () {
      this.addDialogVisible = true
      this.showRoleFatherName()
    },
    // 展示编辑权限的对话框
    async showEditDialog (roleId) {
      this.showRoleFatherName()
      this.editDialogVisible = true
      const { data: res } = await this.$http.get('getrolebyid', { params: { roleId: roleId } })
      if (res.status !== 200) {
        return this.$message.error('查询权限信息失败')
      }
      this.editForm = res.data
    },
    // 获取一级权限列表
    async showRoleFatherName () {
      const { data: res } = await this.$http.get('getroleselectlist')
      if (res.status !== 200) {
        return this.$message.error('查询权限信息失败')
      }
      this.selectFatherRoleList = res.data
    },
    // 修改权限信息并提交
    editRightInfo () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        //  发起修改用户信息的数据请求
        const { data: res } = await this.$http.post('updaterole', this.editForm)
        if (res.status !== 200) {
          return this.$message.error('更新用户信息失败！')
        }
        //  关闭对话框
        this.editDialogVisible = false
        //  刷新数据列表
        this.getRightList()
        //  提示修改成功
        this.$message.success('更新用户信息成功!')
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
